// ignore_for_file: invalid_use_of_protected_member

import 'dart:math';

import 'package:flutter/material.dart';
import 'package:synchronous_data_example/my_model.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  late final SyncModel model1;
  late final SyncModel model2;

  @override
  void initState() {
    super.initState();

    model1 = SyncModel(
      "uuid_test_model1",
      age: 10,
      name: "SyncModel_Name",
      score: 12.883,
      isBoy: true,
      info: {"info": "info", "typeNum": 20},
      children: [
        ObjectModel(name: "ObjectModel1", age: 18),
        ObjectModel(name: "ObjectModel2", age: 12),
      ],
      object: ObjectModel(name: "ObjectModel", age: 18),
    )
      ..addListener(() {
        if (mounted) setState(() {});
      })
      ..addListener(() {
        print("model1 did receive change notification");
      });

    model2 = SyncModel(
      "uuid_test_model2",
      age: 10,
      name: "SyncModel_Name",
      score: 12.883,
      isBoy: true,
      info: {"info": "info", "typeNum": 20},
      children: [
        ObjectModel(name: "ObjectModel1", age: 18),
        ObjectModel(name: "ObjectModel2", age: 12),
      ],
      object: ObjectModel(name: "ObjectModel", age: 18),
    )
      ..addListener(() {
        if (mounted) setState(() {});
      })
      ..addListener(() {
        print("model2 did receive change notification");
      });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(title: const Text('Plugin example app')),
        body: Column(children: [
          Container(height: 40),
          Text('Running on:-->${model1.name}'),
          Text('Running on:-->${model2.name}'),
          TextButton(
            onPressed: () {
              model1.name = "flutter_model_name:${Random().nextInt(10000)}";
              model1.notifyListeners().then((value) {
                print("flutter====notifyListeners");
              });
            },
            child: const Text("修改值"),
          ),
          TextButton(
            onPressed: () {
              model2.name = "flutter_model_name:${Random().nextInt(10000)}";
              model2.notifyListeners().then((value) {
                print("flutter====notifyListeners");
              });
            },
            child: const Text("修改值"),
          ),
        ]),
      ),
    );
  }
}
